#Include g:\AHK\git-ahk-lib\Extend.ahk

class Radio {

  events := Map()
  static ON_CHECKED := 0x1, ON_UNCHECKED := 0x2

  __New(_rect, bg, fg, mg, checked := 0) {
    this._rect := _rect, this.bg := bg, this.fg := fg, this.mg := mg, this.checked := checked
  }

  __Get(key, Params) => this[key]

  SetBG(color) => this.bg := color
  SetFG(color) => this.fg := color

  OnPressed() => this.pressed := 1
  OnRelease() => this.pressed := 0

  OnEvent(event, cb) => this.events.Set(event, cb)

  OnChecked() {
    this.checked := 1, this.pressed := 0
    this.events.Get(Radio.ON_CHECKED, Noop)(this)
  }

  OnUnChecked() {
    this.checked := 0, this.pressed := 0
    this.events.Get(Radio.ON_UNCHECKED, Noop)(this)
  }

  Draw(canvas, dx := 0, dy := 0) {
    c := this.pressed ? this.mg : (this.checked ? this.fg : this.bg)
    canvas.FillRectangle(this._rect.x + dx, this._rect.y + dy, this._rect.w, this._rect.h, c)
  }
}